//
//  HEDiagnosisCardCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/21.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDiagnosisCardCell.h"
#import "HEAIMsgModel.h"

#import "HEDiseaseItemCell.h"

@interface HEDiagnosisCardCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLb;

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) HEAIMsgModel *testModel;
@property (nonatomic, strong) NSIndexPath *indexPath;

@property (nonatomic, strong) UILabel *timeLb;

@property (nonatomic, strong) UIView *warp;

@end

@implementation HEDiagnosisCardCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = CViewBgColor;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _timeLb = [UILabel new];
    _timeLb.font = DYFont(15);
    _timeLb.textAlignment = NSTextAlignmentCenter;
    _timeLb.textColor = CNavDargGrayColor;
    [self.contentView addSubview: _timeLb];
    
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.right.equalTo(self.contentView);
    }];
    
    
    // 商品图片
    _leftImageView = [UIImageView new];
    _leftImageView.image = IMAGE_NAMED(@"home_ai");
    _leftImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_leftImageView];
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];
    _warp = warp;
    
    warp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    warp.layer.shadowOffset = CGSizeMake(0,5);
    warp.layer.shadowOpacity = 1;
    warp.layer.shadowRadius = 6;
    warp.layer.cornerRadius = 10;
    
    _titleLb = [UILabel new];
    _titleLb.text = @"疾病自诊";
    _titleLb.font = DYBlodFont(18);
    _titleLb.textColor = CNavLightBlackColor;
    [warp addSubview:_titleLb];
    
 
    QMUIButton *changeBtn = [[QMUIButton alloc] init];
    changeBtn.imagePosition = QMUIButtonImagePositionRight;// 将图片位置改为在文字下方
    changeBtn.tintColorAdjustsTitleAndImage = CNavBgColor;
    changeBtn.adjustsImageTintColorAutomatically = NO;
    changeBtn.spacingBetweenImageAndTitle = 5;
    [changeBtn setImage:UIImageMake(@"change") forState:UIControlStateNormal];
    [changeBtn setTitle:@"换一换" forState:UIControlStateNormal];
    changeBtn.titleLabel.font = DYFont(16);
    [self.contentView addSubview:changeBtn];
    [changeBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_timeLb.mas_bottom).mas_offset(15);
       make.left.equalTo(self.contentView).mas_offset(15);
       make.width.height.mas_offset(50);
    }];
    [_leftImageView hg_setAllCornerWithCornerRadius: 20];
    
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLb.mas_bottom).mas_offset(15);
        make.left.equalTo(_leftImageView.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset( -(15 + 50 + 5));
//        make.height.mas_greaterThanOrEqualTo(40);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
     
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(15);
        make.left.equalTo(warp).mas_offset(15);
    }];
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
     _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
     _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollsToTop = YES;
    [warp addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(0);
        make.left.equalTo(warp).offset(15);
        make.right.equalTo(warp).offset(-15);
    }];
    
    [_tableView registerClass:HEDiseaseItemCell.class forCellReuseIdentifier: KHEDiseaseItemCell];
    
    
    [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView.mas_bottom).offset(15);
        make.right.equalTo(warp).offset(-15);
        make.bottom.equalTo(warp).mas_offset(-10);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    
    if (formModel == nil) {
        return;
    }
    _delegate = formModel.proxyVC;
    self.testModel = formModel.zx_model;
    
    _timeLb.text = self.testModel.createDate;

    // refreshContent 会走两次 但只有第一次计算的是正确的，故以此方法解决 奇怪
    CGFloat tableViewHeight = [self.testModel getTableHeight:_tableView];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
       make.height.mas_equalTo(tableViewHeight);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView reloadData];
    
}
 

#pragma mark -

- (void)changeClick {
    if ([_delegate respondsToSelector:@selector(reloadCellDataForModel:)]) {
        [_delegate reloadCellDataForModel:self.formModel];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  HEDiseaseItemCell *cell = [tableView dequeueReusableCellWithIdentifier: KHEDiseaseItemCell];
  HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];
  [cell configCellWithModel:model];
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.testModel.subDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];

    CGFloat cellHeight = [tableView fd_heightForCellWithIdentifier: KHEDiseaseItemCell cacheByIndexPath:indexPath configuration:^(HEDiseaseItemCell *cell) {
        [cell configCellWithModel:model];
            }];
    return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];
    if ([_delegate respondsToSelector:@selector(selectDiesaseWithModel:)]) {
        [_delegate selectDiesaseWithModel:model];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  return YES;
}

 


@end
